﻿$.fn.validate = function () {
    var result = true;
    $(this).find(":input").each(function () {
        var elm = $(this);

        //验证不为空
        var required = elm.attr("require");
        if (required == "true") {
            if ($.trim(elm.val()) == "") {
                v_ShowMessage(elm, "该项不能为空！");
                result = false;
                return;
            } else {
                v_HiddenMessage(elm);
            }
        }

        //验证数值
        var isnum = elm.attr("isnum");
        if ($.trim(elm.val()) != "") {
            if (isnum == "number") {
                if (!/^-?\d+(\.{1}\d+)?$/.test(elm.val())) {
                    v_ShowMessage(elm, "请输入正确的数值");
                    result = false;
                    return;
                }
            } else if (isnum == "int") {
                if (!/^-?\d+$/.test(elm.val())) {
                    v_ShowMessage(elm, "请输入正确的整数");
                    result = false;
                    return;
                }
            }
            v_HiddenMessage(elm);
        }

        //验证邮箱
        var mobile = elm.attr("email");
        if (mobile == "true") {
            if ($.trim(elm.val()) != "") {
                if (!/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(elm.val())) {
                    v_ShowMessage(elm, "请输入正确的电子邮箱");
                    result = false;
                    return;
                }
            }
            v_HiddenMessage(elm);
        }

        //验证手机号码
        var mobile = elm.attr("mobile");
        if (mobile == "true") {
            if ($.trim(elm.val()) != "") {
                if (!/^1\d{10}$/g.test(elm.val())) {
                    v_ShowMessage(elm, "请输入正确的手机号码");
                    result = false;
                    return;
                }
            }
            v_HiddenMessage(elm);

        }

        //验证密码相同
        var forkey = elm.attr("forkey");
        if (forkey) {
            if (elm.val() != $("#" + forkey).val()) {
                v_ShowMessage(elm, "两次密码输入不一样，请重新输入");
                result = false;
                return;
            }
        }

        //匹配正则表达式
        var regexp = elm.attr("regexp");
        if (regexp) {
            var reg = new RegExp(elm.attr("regexp").split("|")[0], "g");
            var msg = elm.attr("regexp").split("|")[1];
            if ($.trim(elm.val()) != "") {
                if (!reg.test(elm.val())) {
                    v_ShowMessage(elm, msg);
                    result = false;
                    return;
                }
            }
            v_HiddenMessage(elm);

        }
    });
    return result;
    //alert("aaa");
}

function v_HiddenMessage(elm) {
    if (elm.next(".v_message").is("span")) {
        elm.next(".v_message").remove();
    }
}

function v_ShowMessage(elm, message) {
    if (elm.next(".v_message").is("span")) {
        elm.next(".v_message").html(message);
    } else {
        elm.after("<span class=\"v_message\" style=\"color:#ff0000;\">" + message + "</span>");
    }
    myfocus(elm.attr("id"));
}

function myfocus(myid) {
    var isNav = (window.navigator.appName.toLowerCase().indexOf("netscape") >= 0);
    var isIE = (window.navigator.appName.toLowerCase().indexOf("microsoft") >= 0);
    if (isNav) {
        document.getElementById(myid).focus(); // 获取焦点   

    } else {
        setFocus.call(document.getElementById(myid));
    }
}
function setFocus() {
    var range = this.createTextRange(); //建立文本选区 
    range.moveStart('character', this.value.length); //选区的起点移到最后去
    range.collapse(true);
    range.select();
}